package top.infopub.job.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

import static top.infopub.job.util.PwdMD5Utils.interfacePwdMD5;

public class NCUtil {
    public static JSONObject getOrgInfo(String page, String pageSize,String orgid,String parentid, String startdate ) {
        HttpClient client = HttpClients.createDefault();
        // 要调用的接口方法
        String url = "http://proxy.zhongnangroup.cn/znapicloud/api/org/getOrgInfo";
        HttpPost post = new HttpPost(url);
        JSONObject jsonObject = null;
        try {
            List<NameValuePair> params=new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("code","lwsmz"));
            params.add(new BasicNameValuePair("passwd",interfacePwdMD5()));
            params.add(new BasicNameValuePair("page",page));
            params.add(new BasicNameValuePair("pageSize",pageSize));
            //params.add(new BasicNameValuePair("iseffect","Y"));
            if(StringUtils.isNotEmpty(orgid)){
                params.add(new BasicNameValuePair("orgid",orgid));
            }
            if(StringUtils.isNotEmpty(parentid)){
                params.add(new BasicNameValuePair("parentid",parentid));
            }
            if(StringUtils.isNotEmpty(startdate)){
                params.add(new BasicNameValuePair("startdate",startdate));
            }
            UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(params,"utf8");

            post.setEntity(formEntity);
            HttpResponse res = client.execute(post);
            String response1 = EntityUtils.toString(res.getEntity());
            System.out.println(response1);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //String result = EntityUtils.toString(res.getEntity());// 返回json格式：
                jsonObject = JSONObject.parseObject(response1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonObject;
    }


    public static JSONObject getUserInfo(String page,String pageSize,String orgid,String startdate ) {
        HttpClient client = HttpClients.createDefault();
        // 要调用的接口方法
        String url = "http://proxy.zhongnangroup.cn/znapicloud/api/user/getUserInfo";
        HttpPost post = new HttpPost(url);
        JSONObject jsonObject = null;
        try {
            List<NameValuePair> params=new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("code","lwsmz"));
            params.add(new BasicNameValuePair("passwd",interfacePwdMD5()));
            params.add(new BasicNameValuePair("page",page));
            params.add(new BasicNameValuePair("pageSize",pageSize));
            //params.add(new BasicNameValuePair("iseffect","Y"));
            if(StringUtils.isNotEmpty(startdate)){
                params.add(new BasicNameValuePair("startdate",startdate));
            }
            if(StringUtils.isNotEmpty(orgid)){
                params.add(new BasicNameValuePair("orgid",orgid));
            }
            UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(params,"utf8");

            post.setEntity(formEntity);
            HttpResponse res = client.execute(post);
            String response1 = EntityUtils.toString(res.getEntity());
            System.out.println(response1);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //String result = EntityUtils.toString(res.getEntity());// 返回json格式：
                jsonObject = JSONObject.parseObject(response1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonObject;
    }
}
